<?php

class ShareController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	//设置首页引用js
        $this->view->headScript()->appendFile($this->view->baseUrl().'/js/index_flash.js');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/share.css');
    	include 'top/TopClient.php';
    	include 'top/RequestCheckUtil.php';
    	include 'top/request/ItemGetRequest.php';
    	include 'top/request/TaobaokeItemsDetailGetRequest.php';
    	include 'top/request/TaobaokeItemsConvertRequest.php';
    }

    public function indexAction()
    {
        // action body
        include_once 'TopSdk.php';
        $form=new Application_Form_Share();
        $this->view->form=$form;
	    $db_share=new Application_Model_DbTable_Share();
        if($this->getRequest()->isPost()){
        	$formData=$this->getRequest()->getPost();
        	if($form->isValid($formData)){
        		$share_url=$form->getValue('share_url');
        		if(!$this->validateUrl($share_url)){
        			$this->view->message="这个url有问题哦，请重新输入";
        		}else{
        		////
        		$itemid=Txy_Util_GetItemId::getItemId($share_url);
        		if(0!=$itemid){
        			$db_taobaoitems=new Application_Model_DbTable_TaobaoItems();
        			if(!$db_taobaoitems->isRepeat($itemid)){
        				$this->view->message="该商品已经存在";
        			}else{
						if($db_share->isRepeat($itemid)){
		        			$c = new TopClient;
					        $c->appkey = '12532098';
					        $c->secretKey = 'c22aee3f68fe4acc9015a1e9dba1cd16';
					        $req_item1 = new ItemGetRequest;
					        $req_item1->setFields("detail_url,num_iid,title,pic_url,price");
					        $req_item1->setNumIid($itemid);
		    				$resp_item1=$c->execute($req_item1);
		    				$data=array();
		    				if(isset($resp_item1->item)){
		    					$item=$resp_item1->item;
		    					$data['numiid']=(string)$item->num_iid;
		    					$data['title']=(string)$item->title;
		    					$data['picurl']=(string)$item->pic_url;
		    					$data['price']=(string)$item->price;
		    					$data['taobao_url']=(string)$item->detail_url;
		    					$data['is_taobaoke']=0;
		    					$data['is_approve']=0;
		    					$data['add_time']=new Zend_Db_Expr('now()');
			    				$req_item2=new TaobaokeItemsDetailGetRequest;
			    				$req_item2->setFields("click_url,num_iid");
			    				$req_item2->setNumIids($itemid);
			    				$resp_item2 = $c->execute($req_item2);
			    				if(isset($resp_item2->taobaoke_item_details->taobaoke_item_detail)){
			    					$data['taobao_url']=(string)$resp_item2->taobaoke_item_details->taobaoke_item_detail->click_url;
			    					$data['is_taobaoke']=1;
			    				}
			    				$db_share->addNewShare($data);
			    				$this->view->message="添加成功";
		    				}else{
		    					$this->view->message="商品没找到";
		    				}
						}else{
							$this->view->message="该商品已经分享过了";
						}
        			}
        			//$form->getElement('share_url')->setValue("");
        			$form=new Application_Form_Share();
        		}else{
        			$this->view->message="商品没找到";
        		}
        		////
        		}
        	}
        }
    	//设置title
		$this->view->headTitle("分享精品玉器饰品");
		//设置关键词（seo）
		$this->view->headMeta()->appendName('keywords', "玉吊坠,玉手镯,玉石商品,和田玉,翡翠,岫玉,玉石礼品");
		//设置描述（seo）
		$this->view->headMeta()->appendName('description',"分享精品玉器、分享美玉饰品、分享翡翠饰品、岫玉工艺品、和田玉饰品、各类玉石商品及玉石礼品。");
        //show list
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
        $db = Zend_Db::factory($config->resources->db);
        $selecter=$db->select()->from('txy_share_items',array('title','picurl','price','taobao_url','is_approve'))
        			->order('add_time desc');
        $adapter = new Zend_Paginator_Adapter_DbSelect($selecter);
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(48);
        $paginator->setCurrentPageNumber($this->_getParam('page'));
        $this->view->paginator = $paginator;
    }
    private function validateUrl($share_url){
    	if(!filter_var($share_url,FILTER_VALIDATE_URL)){
        	return false;
    	}
    	$parse = parse_url($share_url);
    	if('detail.tmall.com'!=$parse['host']&&'item.taobao.com'!=$parse['host']){
    		return false;
    	}
    	return true;
    }


}

